<!DOCTYPE html>
<html>
<head>
<title>Test the File System Access API during prerendering</title>
</head>
<body>
<script>

const eventsSeen = [];

let resolveFunc;
let rejectFunc;

document.addEventListener('prerenderingchange', () => {
  eventsSeen.push(
      `prerenderingchange (prerendering: ${document.prerendering})`);
});

const result = new Promise((resolve, reject) => {
  resolveFunc = resolve;
  rejectFunc = reject;
});

async function startShowOpenFilePicker() {
  try {
    // The page should initially be in the prerendering state.
    eventsSeen.push(
        `startShowOpenFilePicker (prerendering: ${document.prerendering})`);

    const [fileHandle] = await window.showOpenFilePicker();

    // showOpenFilePicker() is expected to be deferred until the prerendered
    // page is activated. So `document.prerendering` should be false here.
    eventsSeen.push(
        `showOpenFilePicker (prerendering: ${document.prerendering})`);

    resolveFunc(fileHandle.name);
  } catch (err) {
    rejectFunc(err);
  }
}

function runLoop() {
  return new Promise(resolve => {
    setTimeout(resolve, 0);
  });
}

</script>
</body>
</html>
